package com.hhkj.admin.sale.common.component.supplier;

import cn.hutool.core.util.StrUtil;
import com.hhkj.admin.sale.common.utils.InvitationQRCodeUtils;
import com.hhkj.admin.sale.domain.Sale;
import com.hhkj.admin.sale.service.base.SaleService;
import com.hhkj.admin.sale.service.extend.SaleExtendService;
import com.ruoyi.common.exception.GlobalException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * SaleSupplier
 *
 * @author HHKJ-SERVER-ZJ
 * @since 2023/10/26 16:18
 */
@Component
public class SaleSupplier {

    @Autowired
    private SaleExtendService saleExtendService;
    @Autowired
    private SaleService saleService;

    /**
     * 获取邀请码信息
     *
     * @param id 销售id
     * @return 邀请码阿里云url地址
     */
    public String getInvitationCodeUrlById(Long id) {
        Sale salePo = saleExtendService.getByIdNotNull(id);
        if (StrUtil.isNotBlank(salePo.getInvitationCodeUrl())) {
            return salePo.getInvitationCodeUrl();
        }
        // 生成邀请码地址
        String invitationCodeUrl = InvitationQRCodeUtils.generateAliYunUrl(salePo.getNum());
        if (StrUtil.isBlank(invitationCodeUrl)) {
            throw new GlobalException("邀请码生成失败，请联系管理员");
        }
        // 更新销售信息
        salePo.setInvitationCodeUrl(invitationCodeUrl);
        saleService.updateById(salePo);
        return invitationCodeUrl;
    }
}
